/**
 * 
 */
package extraStuff;

import java.io.IOException;
import java.util.Scanner;

import Game.GameClass;

import cardCollections.Hand;
import cardCollections.Shoe;

/**
 * @author CAT
 * A test of the game logic of the class
 */
public class ConsoleGame {
	Scanner scan = new Scanner(System.in);

	private GameClass playGame;

	/**
	 * @param args
	 * @throws IOException 
	 * Creates a console based game with two players
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("Welcome to black jack test");
		System.out
				.println("This will be a standard game with two players and one dealer");
		ConsoleGame letsPlay = new ConsoleGame();
		for (int i = 0; i < 5; i++) {
			letsPlay.runGame();
		}

	}

	/**
	 * @throws IOException
	 * Creates a new Gameclass with two players
	 */
	public ConsoleGame() throws IOException {
		playGame = new GameClass(2);
	}

	/**
	 * @param action, the string input from the user that represents their action
	 * @param playerIndex, the index of the player in the game
	 * @throws IOException
	 */
	public void UserAction(String action, int playerIndex) throws IOException {
		if (action.contains("hit")){
			playGame.hit(playerIndex);
		System.out.println("Player " + (playerIndex + 1) + " hand: "
				+ playGame.getHand(playerIndex));
		}
		else if (action.contains("stand")){
			playGame.stand(playerIndex);
		}
		//if (action.contains("split"))
			//playGame.splitHand(playerIndex);
		else if (action.contains("double")) {
			playGame.Doubledown(playerIndex);			
		} else{
			System.out.println("Screw you. Enter Something Do-able");
			UserAction(scan.nextLine(), playerIndex);			
		}
		

	}

	/**
	 * @throws IOException
	 * prints out all players hand
	 * Until everyone either stands or busts, asks users if they would like to stand, hit, etc
	 */
	public void runGame() throws IOException {
		System.out.println("\n\n\nNew Game!!!!!!");
		playGame.deal();
		for (int i = 0; i < playGame.getTotalPlayers(); i++) {
			System.out.println("player " + (i + 1) + " hand: "
					+ playGame.getHand(i).toString() + " ");
		}
		System.out.println("Dealer hand: "
				+ playGame.getDealerHand().toString() + "\n");
		while (!playGame.isEveryoneDone()) {
			for (int i = 0; i < playGame.getTotalPlayers(); i++) {
				if (!playGame.getHand(i).isStanding()&&!playGame.getHand(i).isBusted()) {
					System.out.println("\nplayer " + (i + 1) + " hand: "
							+ playGame.getHand(i).toString());
					System.out.print("What would player " + (i + 1)
							+ " like to do\n");					
					String playerAction = scan.nextLine();
					this.UserAction(playerAction, i);					
				}
			}
			playGame.dealer();
			System.out.println("Dealer hand: "
					+ playGame.getDealerHand().toString() + "\r\n");
		}
		checkIfPlayersWon();
	}

	
	/**
	 * Checks if each player won, tied, or lost to the dealer
	 */
	private void checkIfPlayersWon() {
		for (int i = 0; i < playGame.getTotalPlayers(); i++) {
			if(playGame.checkWin(i)==-1){
				System.out.println("Player "+ (i+1) + " won");				
			}
			else if(playGame.checkWin(i) == 0){
				System.out.println("Player "+ (i+1)+ " tied");
			}
			else{
				System.out.println("Player "+ (i+1)+ " lost");
			}
				

		}
	}
}
